package homework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Doudizhu {

    ArrayList<Puke> pukeArrayList = new ArrayList<Puke>();



    /**
     * 初始化
     */
    public void init(){
        pukeArrayList.add(new Puke("","大王",17));
        pukeArrayList.add(new Puke("","小王",16));

        String point = null;
        String type = null;
        int size = 0;

        for (int i = 0; i < 4; i++){
            if (i == 0){
                type = "梅花";
            }else if (i == 1){
                type = "黑桃";
            }else if (i == 2){
                type = "方片";
            }else{
                type = "红心";
            }
            for (int j = 2; j < 15; j++){
                size = j;
                point = j + "";
                if (j == 11){
                    point = "J";
                }else if (j == 12){
                    point = "Q";
                }else if (j == 13){
                    point = "K";
                }else if (j == 14){
                    point = "A";
                }else if (j == 2){
                    size = 15;
                }
                pukeArrayList.add(new Puke(point,type,size));
            }
        }




    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(pukeArrayList);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        ArrayList<Puke> player1 = new ArrayList<>(pukeArrayList.subList(0,17));
        ArrayList<Puke> player2 = new ArrayList<>(pukeArrayList.subList(17,34));
        ArrayList<Puke> player3 = new ArrayList<>(pukeArrayList.subList(34,51));

        sort(player1);
        sort(player2);
        sort(player3);

        System.out.println("拥有" + pukeArrayList.get((int) (Math.random() * 52)) + "的人开始叫地主");
        System.out.println("地主牌为" + pukeArrayList.get(51) + "," + pukeArrayList.get(52) + "," + pukeArrayList.get(53));
        System.out.println("\n玩家1的牌为：");
        for (Puke puke : player1){
            System.out.print(puke + ",");
        }
        System.out.println("\n\n玩家2的牌为：");
        for (Puke puke : player2){
            System.out.print(puke + ",");
        }
        System.out.println("\n\n玩家3的牌为：");
        for (Puke puke : player3){
            System.out.print(puke + ",");
        }

    }


    public void sort(ArrayList arrayList){
        Collections.sort(arrayList, new Comparator<Puke>() {
            @Override
            public int compare(Puke o1, Puke o2) {
                return (o1.getSize() - o2.getSize());
            }
        });
    }


}
